package com.laihui.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.laihui.Constants;
import com.laihui.MyApplication;
import com.laihui.base.BaseActivity;
import com.laihui.entity.MemberBank;
import com.laihui.entity.RetMap;
import com.laihui.view.TopBar;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BankCardActivity extends BaseActivity {


    @BindView(R.id.iv_background_bank_card)
    ImageView ivBackgroundBankCard;
    @BindView(R.id.iv_bank_logo)//银行卡logo
            ImageView ivBankLogo;
    @BindView(R.id.tv_bank_name)//银行卡名字
            TextView tvBankName;
    @BindView(R.id.btn_next)//银行卡号
            TextView btn_next;

    @BindView(R.id.tv_bank_number)//银行卡号
            TextView tvBankNumber;
    @BindView(R.id.topbar)//topbar
            TopBar topbar;
    private Activity mActivity;
    private MyApplication application;
    private static final String TAG = "BankCardActivity";

    @Override
    public void init() {
        mActivity = BankCardActivity.this;
        application = (MyApplication) getApplication();
        topbar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
            }
        });
        bankcard();
    }

    private void bankcard() {
        Map<String, String> map = new HashMap<>();
        map.put("member_id", application.userId);
        application.apiService.isBankBind(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MemberBank>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: "+e.toString());
                    }

                    @Override
                    public void onNext(MemberBank memberBank) {
                        tvBankName.setText(memberBank.item.bank);
                        Glide.with(mActivity).load(Constants.BASE_URL+memberBank.item.bank_img_mobile).into(ivBankLogo);

                        //tvBankNumber.setText(bankCard.getBankCard().getBank_cardno());
                        if (memberBank.item.bank_cardno.length()==16){
                            tvBankNumber.setText("**************"+memberBank.item.bank_cardno.substring(12,16));
                        }else{
                            tvBankNumber.setText("**************"+memberBank.item.bank_cardno.substring(15,19));
                        }

                    }
                });

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, UnbingBankCardActivity.class);
                intent.putExtra("state","2");
                startActivityForResult(intent,4);
            }
        });
        getUnBindData();
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                btn_next.setText("解绑申请中");
                btn_next.setClickable(false);
        }
    }
    private void getUnBindData() {
            Map<String, String> map = new HashMap<>();
            map.put("member_id", application.userId);
            application.apiService.unbindBankCard(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<RetMap>() {
                        @Override
                        public void onCompleted() {

                        }
                        @Override
                        public void onError(Throwable e) {
                            Log.e(TAG, "onError: "+e.toString());
                        }


                        public void onNext(RetMap retMap) {
                            if (retMap.status.equals("1")){
                                btn_next.setText("解绑申请中");
                                btn_next.setClickable(false);
                            }
                        }
                    });

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_bank_card;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }

}
